package com.easyschool.model.bo;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import com.easyschool.model.user.User;

public class BoUser {
	
	private static BoUser instance;
	private BoUser(){}
	
	public static BoUser getInstance(){
		if(instance == null) instance = new BoUser();
		return instance;
	}
	
	
	private static SessionFactory sessionFactory = null;
	public List<User> getAllUsers() throws Exception
	{		
		Session session = null;
		try 
		{
			try
			{
				sessionFactory = BoFactory.getSessionFactory();
				session = sessionFactory.openSession();
				
				
				Transaction tx = session.beginTransaction();
				
				@SuppressWarnings("unchecked")
				List<User> list = (List<User>)session.createCriteria(User.class).list();
				
				tx.commit();
								
				return deleteTwin(list);
				
			}
			catch (Exception e)
			{
				throw new Exception(e);
			}
		}
		finally
		{
			session.close();
		}
	}
	
	private List<User> deleteTwin(List<User> list){
		return new ArrayList<User>(new HashSet<User>(list));
	}
	
	public User getUserById(Integer idUser) throws Exception
	{		
		Session session = null;
		try 
		{
			try
			{
				sessionFactory = BoFactory.getSessionFactory();
				session = sessionFactory.openSession();
				
				
				Transaction tx = session.beginTransaction();
				
				@SuppressWarnings("unchecked")
				User user = (User)session.createCriteria(User.class).add(Restrictions.idEq(idUser)).uniqueResult();
				
				tx.commit();
				
				return user;
				
			}
			catch (Exception e)
			{
				throw new Exception(e);
			}
		}
		finally
		{
			session.close();
		}
	}
	
	public List<User> getAllUsersByFilters(String name, String surname, Date birthday, boolean isProfessor) throws Exception
	{		
		Session session = null;
		try 
		{
			try
			{
				sessionFactory = BoFactory.getSessionFactory();
				session = sessionFactory.openSession();
				
				
				Transaction tx = session.beginTransaction();
				
				Criteria crit = session.createCriteria(User.class);
				
				if(name != null){
					crit.add( Restrictions.like("name", "%"+name+"%") );
				}
				
				if(surname != null){
					crit.add( Restrictions.like("surname", "%"+surname+"%") );
				}
				
				if(birthday != null){
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					crit.add( Restrictions.like("birthday",birthday) );
				}
				
				if(isProfessor){
					crit.add( Restrictions.eq("isProfessor", true));
				}
				
				@SuppressWarnings("unchecked")
				List<User> list = crit.list();
				
				tx.commit();
				
				return deleteTwin(list);
				
			}
			catch (Exception e)
			{
				throw new Exception(e);
			}
		}
		finally
		{
			session.close();
		}
	}
	
	public Integer addUser(User user) throws Exception
	{		
		Session session = null;
		try 
		{
			try
			{
				sessionFactory = BoFactory.getSessionFactory();
				session = sessionFactory.openSession();
				
				
				Transaction tx = session.beginTransaction();
				
				Integer id = (Integer)session.save(user);
				
				tx.commit();
				
				return id;
				
			}
			catch (Exception e)
			{
				throw new Exception(e);
			}
		}
		finally
		{
			session.close();
		}
	}
	
	public void updateUser(User user) throws Exception
	{		
		Session session = null;
		try 
		{
			try
			{
				sessionFactory = BoFactory.getSessionFactory();
				session = sessionFactory.openSession();
				
				
				Transaction tx = session.beginTransaction();
				
				session.update(user);
				
				tx.commit();
				
				
				
			}
			catch (Exception e)
			{
				throw new Exception(e);
			}
		}
		finally
		{
			session.close();
		}
	}
	
	public Integer removeUser(Integer id) throws Exception{
		
		Session session = null;
		try 
		{
			try
			{
				sessionFactory = BoFactory.getSessionFactory();
				session = sessionFactory.openSession();
				
				
				Transaction tx = session.beginTransaction();
				
				session.delete(new User(id, null,null,null,null, null, null));
				
				tx.commit();
				
				return id;
				
			}
			catch (Exception e)
			{
				throw new Exception(e);
			}
		}
		finally
		{
			session.close();
		}
		
		
	
	}
	
	public void addTeachClasses(Integer idUser, Integer idClass) throws Exception{
		Session session = null;
		try 
		{
			try
			{
				sessionFactory = BoFactory.getSessionFactory();
				session = sessionFactory.openSession();
				
				
				User teacher = getUserById(idUser);
				com.easyschool.model.school.Class theClass = BoClass.getInstance().getClassById(idClass);
				
				teacher.getClasses().add(theClass);
				
				Transaction tx = session.beginTransaction();
				
				session.update(teacher);
				
				tx.commit();
				
				
			}
			catch (Exception e)
			{
				throw new Exception(e);
			}
		}
		finally
		{
			session.close();
		}
	}
	
	public void removeTeachClasses(Integer idClass, Integer idUser) throws Exception{
		
		Session session = null;
		try 
		{
			try
			{
				sessionFactory = BoFactory.getSessionFactory();
				session = sessionFactory.openSession();
				
				
				Transaction tx = session.beginTransaction();
				
				User teacher = getUserById(idUser);
				com.easyschool.model.school.Class theClass = BoClass.getInstance().getClassById(idClass);
				
				teacher.getClasses().remove(theClass);
				
				session.update(teacher);
				
				tx.commit();
				
				
			}
			catch (Exception e)
			{
				throw new Exception(e);
			}
		}
		finally
		{
			session.close();
		}
		
		
	
	}
}
